package com.resolution.dietaryassessment.view.tcm.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.resolution.dietaryassessment.R;
import com.resolution.dietaryassessment.view.tcm.bean.MyTCMConstant;

import java.util.ArrayList;
import java.util.List;

/**
 * @author By Resolusion
 * @Title:
 * @Date: 2020/2/23
 * @Time: 15:44
 * @email Res01ution@foxmail.com
 */
public class SubjectAdapter extends RecyclerView.Adapter<SubjectAdapter.ViewHolder> {
    private List<String> mTCMSubjectList;

    private GetScore getScore = null;
    private static List<Integer> scoreList = new ArrayList<>();

    public SubjectAdapter(List<String> beanList) {
        mTCMSubjectList = beanList;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        View tcmSubjectView;
        TextView tcmSubjectName;

        RadioGroup radioGroup;
        RadioButton radioBtn1;
        RadioButton radioBtn2;
        RadioButton radioBtn3;
        RadioButton radioBtn4;
        RadioButton radioBtn5;

        ViewHolder(View view) {
            super(view);
            tcmSubjectView = view;
            tcmSubjectName = view.findViewById(R.id.subject);

            radioGroup = view.findViewById(R.id.tcm_group);
            radioGroup.setOnCheckedChangeListener((group, checkedId) -> {
                int multiple = 0;
                switch (checkedId){
                    case R.id.radioNone:
                        multiple = MyTCMConstant.NONE_STATE;
                        break;
                    case R.id.radioSeldom:
                        multiple = MyTCMConstant.SELDOM_STATE;
                        break;
                    case R.id.radioSometimes:
                        multiple = MyTCMConstant.SOMETIME_STATE;
                        break;
                    case R.id.radioOften:
                        multiple = MyTCMConstant.OFTEN_STATE;
                        break;
                    case R.id.radioAlways:
                        multiple = MyTCMConstant.ALWAYS_STATE;
                        break;
                    default:
                }
                scoreList.set(getLayoutPosition(),multiple);
            });
            radioBtn1 = view.findViewById(R.id.radioNone);
            radioBtn2 = view.findViewById(R.id.radioSeldom);
            radioBtn3 = view.findViewById(R.id.radioSometimes);
            radioBtn4 = view.findViewById(R.id.radioOften);
            radioBtn5 = view.findViewById(R.id.radioAlways);
        }
    }

    public void setScore(GetScore score){
        getScore = score;
    }

    public void getScore(){
        //总倍数
        int totalMultiple = 0;
        for (int i : scoreList){
            totalMultiple += i;
        }
        getScore.getMultiple(totalMultiple);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_recycler_tcm, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String TCMSubject = mTCMSubjectList.get(position);
        holder.tcmSubjectName.setText(TCMSubject);
        scoreList.add(MyTCMConstant.SOMETIME_STATE);
    }

    @Override
    public int getItemCount() {
        return mTCMSubjectList.size();
    }

    public void clearList(){
        scoreList.clear();
    }
}